package com.dadie.dorm.dao.impl;


import com.dadie.dorm.dao.IDormbuildDao;
import com.dadie.dorm.pojo.Dormbuild;
import com.dadie.dorm.pojo.query.DormbuildQuery;
import com.dadie.dorm.pojo.vo.DormbuildCountVO;
import com.dadie.dorm.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DormbuildDaoImpl implements IDormbuildDao {
    @Override
    public List<Dormbuild> selectByPage(DormbuildQuery dormbuildQuery) {
        int offset = (dormbuildQuery.getPage() - 1) * dormbuildQuery.getLimit();
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<Dormbuild> list = new ArrayList<>();
        try {
            connection = JDBCUtil.getConnection();
            // SELECT id,name,password,age,gender FROM dormbuild where name like ? and gender=? limit ?,?
            // SELECT id,name,password,age,gender FROM dormbuild where name like ? limit ?,?
            // SELECT id,name,password,age,gender FROM dormbuild where gender=? limit ?,?
            // SELECT id,name,password,age,gender FROM dormbuild limit ?,?
            //你有这个条件，我们就拼上这个条件 and ...， 但是第一个又没有and
            // String sql = "select * from dormbuild where 1=1 "
            // if(name is not null) {
            //      sql += "and name like ?"
            // }
            // if(gender is not null) {
            //      sql += "and gender=?"
            // }
            String sql = "SELECT id,name,introduce FROM dormbuild where 1=1 ";
            //这里面放所有搜索条件
            List<Object> queryList = new ArrayList<>();
            String queryName = dormbuildQuery.getName();
            if (queryName != null && !"".equals(queryName)) {
                sql += " and name like ? ";
                queryList.add("%" + queryName + "%");
            }

            sql += " limit ?,?";
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < queryList.size(); i++) {
                preparedStatement.setObject(i + 1, queryList.get(i));
            }
            preparedStatement.setInt(queryList.size() + 1, offset);
            preparedStatement.setInt(queryList.size() + 2, dormbuildQuery.getLimit());

            System.out.println(preparedStatement);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {//判断下一个有没有，如果返回true而且指向下一个，没有返回false
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String introduce = resultSet.getString("introduce");



                Dormbuild dormbuild = new Dormbuild(id, name, introduce);
                list.add(dormbuild);
            }
            for (Dormbuild dormbuild : list) {
                System.out.println(dormbuild);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.close(connection, preparedStatement, resultSet);
        }

        return list;
    }

    @Override
    public Integer selectTotalCount(DormbuildQuery dormbuildQuery) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        int totalCount = 0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select count(*) from dormbuild where 1=1 ";
            List<Object> queryList = new ArrayList<>();
            String queryName = dormbuildQuery.getName();
            if (queryName != null && !"".equals(queryName)) {
                sql += " and name like ? ";
                queryList.add(queryName);
            }

            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < queryList.size(); i++) {
                preparedStatement.setObject(i + 1, queryList.get(i));
            }
            System.out.println(preparedStatement);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                totalCount = resultSet.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return totalCount;
    }

    @Override
    public int deleteById(int id) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        int count = 0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "delete from dormbuild where id=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            System.out.println(preparedStatement);
            count = preparedStatement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count;
    }

    @Override
    public int add(Dormbuild dormbuild) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        int count = 0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "insert into dormbuild(name,introduce) values(?,?)";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, dormbuild.getName());
            preparedStatement.setString(2, dormbuild.getIntroduce());


            System.out.println(preparedStatement);
            count = preparedStatement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count;
    }

    @Override
    public List<DormbuildCountVO> selectDormbuildCount() {
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        List<DormbuildCountVO> list=new ArrayList<>();
        try {
            connection= JDBCUtil.getConnection();
            String sql="SELECT d.name,COUNT(*) AS value \n" +
                    "FROM student AS s INNER JOIN dormbuild AS d\n" +
                    "ON s.dormbuild_id=d.id \n" +
                    "GROUP BY d.id";
            preparedStatement=connection.prepareStatement(sql);
            System.out.println(preparedStatement);
            resultSet=preparedStatement.executeQuery();
            while (resultSet.next()){
                String name=resultSet.getString("name");
                int value=resultSet.getInt("value");
                DormbuildCountVO banjiCountVO=new DormbuildCountVO(name,value);
                list.add(banjiCountVO);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }
    @Override
    public Dormbuild getDormbuildUpdatePage(String id) {
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        Dormbuild dormbuild=null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select id,name,introduce from dormbuild where id=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,Integer.parseInt(id));
            resultSet = preparedStatement.executeQuery();

            while (resultSet.next()) {
                String name = resultSet.getString("name");
                String introduce = resultSet.getString("introduce");

                dormbuild = new Dormbuild(Integer.parseInt(id),name,introduce);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtil.close(connection, preparedStatement, resultSet);
        }
        return dormbuild;
    }

    @Override
    public Dormbuild selectById(int id) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        Dormbuild dormbuild = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "SELECT id,name,introduce FROM dormbuild where id=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            System.out.println(preparedStatement);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {//判断下一个有没有，如果返回true而且指向下一个，没有返回false
                String name = resultSet.getString("name");
                String introduce = resultSet.getString("introduce");

                dormbuild = new Dormbuild(id, name,introduce);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.close(connection, preparedStatement, resultSet);
        }

        return dormbuild;
    }


    @Override
    public int update(Dormbuild dormbuild) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        int count=0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "update dormbuild set name=?,introduce=? where id=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, dormbuild.getName());
            preparedStatement.setString(2, dormbuild.getIntroduce());

            preparedStatement.setInt(3,dormbuild.getId());
            count=preparedStatement.executeUpdate();
            System.out.println(preparedStatement);
            System.out.println("count: " + count);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return count;
    }


}
